package com.example.eccdemo;

import android.util.Base64;

import org.spongycastle.jce.interfaces.ECPrivateKey;
import org.spongycastle.jce.interfaces.ECPublicKey;
import org.spongycastle.jce.provider.BouncyCastleProvider;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.security.auth.Destroyable;


public class ECCUtil {
	static {
		Security.addProvider(new BouncyCastleProvider());
	}

	//生成秘钥对
	public static KeyPair getKeyPair() throws Exception {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "SC");
		ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("Secp256k1");
		keyPairGenerator.initialize(ecGenParameterSpec, new SecureRandom());
		return keyPairGenerator.generateKeyPair();
	}

	//获取公钥(Base64编码)
	public static String getPublicKey(KeyPair keyPair) {
		ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
		byte[] bytes = publicKey.getEncoded();
		return Base64Utils.encodeToString(bytes);
	}

	//获取私钥(Base64编码)
	public static String getPrivateKey(KeyPair keyPair) {
		ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
		byte[] bytes = privateKey.getEncoded();
		return Base64Utils.encodeToString(bytes);
	}

	//将Base64编码后的公钥转换成PublicKey对象
	public static ECPublicKey string2PublicKey(String pubStr) throws Exception {
		byte[] keyBytes = Base64Utils.decodeFromString(pubStr);
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("EC", "SC");
		ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec);
		return publicKey;
	}

	//将Base64编码后的私钥转换成PrivateKey对象
	public static ECPrivateKey string2PrivateKey(String priStr) throws Exception {
		byte[] keyBytes = Base64Utils.decodeFromString(priStr);
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("EC", "SC");
		ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec);
		return privateKey;
	}

	//公钥加密
	public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception {
		Cipher cipher = Cipher.getInstance("ECIES", "SC");
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}

	//私钥解密
	public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception {
		Cipher cipher = Cipher.getInstance("ECIES", "SC");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}

	public static void main(String[] args) throws Exception {
		KeyPair keyPair = ECCUtil.getKeyPair();
		String publicKeyStr = ECCUtil.getPublicKey(keyPair);
		String privateKeyStr = ECCUtil.getPrivateKey(keyPair);
		System.out.println("ECC公钥Base64编码:" + publicKeyStr);
		System.out.println("ECC私钥Base64编码:" + privateKeyStr);

		ECPublicKey publicKey = string2PublicKey(publicKeyStr);
		ECPrivateKey privateKey = string2PrivateKey(privateKeyStr);

		byte[] publicEncrypt = publicEncrypt("Hello World!".getBytes(), publicKey);
		String encodeToString = Base64Utils.encodeToString(publicEncrypt);
		System.out.println(encodeToString);
		byte[] privateDecrypt = privateDecrypt(publicEncrypt, privateKey);
		System.out.println(new String(privateDecrypt));
	}

	public static class Base64Utils {
		public static String encodeToString(byte[] key) {
			return new String(android.util.Base64.encode(key, Base64.DEFAULT));
		}

		public static byte[] decodeFromString(String str) {
			return android.util.Base64.decode(str, android.util.Base64.DEFAULT);
		}
	}
}  